<?php
$themename = "主题";
$shortname = "T";
$options = array (
	array(	"name" => "外链图片略缩图开关",
			"desc" => "如果你的文章中使用外链图片,又希望显示这些外链图片的略缩图,则打开此项. 默认关闭.",
			"id" => $shortname."_external",
			"std" => "",
			"type" => "select",
			"options" => array("Off", "On")),

	array(	"name" => "评论表情图片开关",
			"desc" => "控制评论表情图片是否显示. 默认关闭.",
			"id" => $shortname."_smiley",
			"std" => "",
			"type" => "select",
			"options" => array("Off", "On")),

	array(	"name" => "样式切换开关",
			"id" => $shortname."_style",
			"desc" => "为空关闭样式切换，默认为空，格式：样式名|style，如：圣诞|xmas",
			"std" => "",
			"type" => "text"),

	array(	"name" => "首页自定义网页描述",
			"id" => $shortname."_description",
			"desc" => "SEO 可选项",
			"std" => "唯美意境 wwmm.me",
			"type" => "text"),

	array(	"name" => "首页自定义关键词",
			"id" => $shortname."_keywords",
			"desc" => "SEO 可选项",
			"std" => "美图,文字,心情",
			"type" => "text"),
);

function mytheme_add_admin() {
    global $themename, $shortname, $options;
    if ( $_GET['page'] == basename(__FILE__) ) {
        if ( 'save' == $_REQUEST['action'] ) {
                foreach ($options as $value) {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }

                foreach ($options as $value) {
                    if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }

                header("Location: themes.php?page=theme-options.php&saved=true");
                die;

        } else if( 'reset' == $_REQUEST['action'] ) {
            foreach ($options as $value) {
                delete_option( $value['id'] ); }
            header("Location: themes.php?page=theme-options.php&reset=true");
            die;
        }
    }
    add_theme_page($themename." 参数", "主题参数", 'edit_themes', basename(__FILE__), 'mytheme_admin');
}

function mytheme_admin() {
    global $themename, $shortname, $options;
    if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' 设置已保存。</strong></p></div>';
?>

<div class="wrap">
	<?php screen_icon(); ?>
	<h2>唯美意境 <?php echo $themename; ?>参数</h2>
	
	<form method="post">
		<table class="form-table">
		<?php foreach ($options as $value) { 
		switch ( $value['type'] ) {
			case 'text':
		?>
			<tr>
				<th scope="row"><?php echo $value['name']; ?></th>
				<td>
					<input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_option( $value['id'] ) != "") { echo stripslashes( get_option( $value['id'] ) ); } else { echo $value['std']; } ?>" />
					<span class="description"><?php echo $value['desc']; ?></span>
				</td>
			</tr>
		<?php 
			break;
			case 'textarea':
		?>
			<tr>
				<th scope="row"><?php echo $value['name']; ?></th>
				<td>
					<textarea name="<?php echo $value['id']; ?>" style="width:400px; height:200px;" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_option( $value['id'] ) != "") { echo stripslashes( get_option(  $value['id'] ) ); } else { echo $value['std']; } ?></textarea>
					<span class="description"><?php echo $value['desc']; ?></span>
				</td>
			</tr>
		<?php 
			break;
			case 'select':
		?>
			<tr>
				<th scope="row"><?php echo $value['name']; ?></th>
				<td>
					<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
						<?php foreach ($value['options'] as $option) { ?>
						<option<?php if ( get_option( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
						<?php } ?>
						</select>
					<span class="description"><?php echo $value['desc']; ?></span>
				</td>
			</tr>
		<?php
			break;
			case "checkbox":
		?>
			<tr>
				<th scope="row"><?php echo $value['name']; ?></th>
				<td>
					<?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
					<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
					<span class="description"><?php echo $value['desc']; ?></span>
				</td>
			</tr>
		<?php
			break;
		}}
		?>
		</table>
		<p class="submit">
			<input type="hidden" name="action" value="save" />
			<input name="save" type="submit" class="button-primary" value="保存更改" />    
		</p>
	</form>
	<form method="post">
		<input type="hidden" name="action" value="reset" />
		<input name="reset" type="submit" class="button-secondary action" value="恢复默认设置" />
	</form>
</div>
<?php }add_action('admin_menu', 'mytheme_add_admin'); ?>